package com.student.dao.impl;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.student.dao.StudentDao;
import com.student.entity.Student;
import com.student.entity.User;
import com.student.template.JdbcTemplate;

public class StudentDaoImpl implements StudentDao {

	@Override
	public void insert(Student student) {
		JdbcTemplate<User> jdbcTemplate = new JdbcTemplate<>();
		jdbcTemplate.update((conn) -> {
			PreparedStatement pst = null;
			String sql = "insert into student(sname,studentNumber, birthday,sex,polity,brief) values(?,?,?,?,?,?)";
			pst = conn.prepareStatement(sql);
			pst.setString(1,student.getSname());
			pst.setString(2, student.getStudentNumber());
			pst.setDate(3, new Date(student.getBirthday().getTime()));
			pst.setInt(4, student.getSex());
			pst.setInt(5, student.getPolity());
			pst.setString(6, student.getBrief());
			pst.executeUpdate();
		});
	}

	@Override
	public List<Student> findList(Integer currentPage, Integer pageCount, String likename) {
		JdbcTemplate<List<Student>> jdbcTemplate = new JdbcTemplate<>();
		return jdbcTemplate.query((conn) -> {
			PreparedStatement pst = null;
			ResultSet rs = null;
			List<Student> list = new ArrayList<>();
			try {
				String sql = "select * from student where sname like ?  limit " + (currentPage - 1) * pageCount + ", " + pageCount + "";
				pst  = conn.prepareStatement(sql);
				pst.setString(1, "%" + likename + "%");
				rs = pst.executeQuery();
				while(rs.next()) {
					Student student = new Student();
					student.setId(rs.getInt("id"));
					student.setSname(rs.getString("sname"));
					student.setStudentNumber(rs.getString("studentNumber"));
					student.setBirthday(rs.getDate("birthday"));
					student.setSex(rs.getInt("sex"));
					student.setPolity(rs.getInt("polity"));
					student.setBrief(rs.getString("brief"));
					list.add(student);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} 
			return list;
		});
	}

	@Override
	public Integer getTotal(String likename) {
		JdbcTemplate<Integer> jdbcTemplate = new JdbcTemplate<>();
		return jdbcTemplate.query((conn) -> {
			PreparedStatement pst = null;
			ResultSet rs = null;
			Integer count = 0;
			try {
				String sql = "select count(1) from student where sname like ?";
				pst  = conn.prepareStatement(sql);
				pst.setString(1, "%" + likename + "%");
				rs = pst.executeQuery();
				if(rs.next()) {
					count = rs.getInt(1);
					
				}
			} catch (Exception e) {
				e.printStackTrace();
			} 
			return count;
		});
	}
	
	@Override
	public void update(Student student) {
		JdbcTemplate<User> jdbcTemplate = new JdbcTemplate<>();
		jdbcTemplate.update((conn) -> {
			PreparedStatement pst = null;
			String sql = "update student set sname=?, studentNumber=?, birthday=?,sex=?,polity=?,brief=? where id = ?";
			pst = conn.prepareStatement(sql);
			pst.setString(1,student.getSname());
			pst.setString(2, student.getStudentNumber());
			pst.setDate(3, new Date(student.getBirthday().getTime()));
			pst.setInt(4, student.getSex());
			pst.setInt(5, student.getPolity());
			pst.setString(6, student.getBrief());
			pst.setInt(7, student.getId());
			pst.executeUpdate();
		});
		
	}

	@Override
	public Student findById(Integer id) {
		JdbcTemplate<Student> jdbcTemplate = new JdbcTemplate<>();
		return jdbcTemplate.query((conn) -> {
			PreparedStatement pst = null;
			ResultSet rs = null;
			Student student = null;
			try {
				String sql = "select * from student where id = ?";
				pst  = conn.prepareStatement(sql);
				pst.setInt(1, id);
				rs = pst.executeQuery();
				if(rs.next()) {
					student = new Student();
					student.setId(rs.getInt("id"));
					student.setSname(rs.getString("sname"));
					student.setStudentNumber(rs.getString("studentNumber"));
					student.setBirthday(rs.getDate("birthday"));
					student.setSex(rs.getInt("sex"));
					student.setPolity(rs.getInt("polity"));
					student.setBrief(rs.getString("brief"));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} 
			return student;
		});
	}

	@Override
	public void batchDelete(int[] ids) {
		JdbcTemplate<User> jdbcTemplate = new JdbcTemplate<>();
		jdbcTemplate.update((conn) -> {
			PreparedStatement pst = null;
			String sql = "delete from student where id = ?";
			pst  = conn.prepareStatement(sql);
			for (int i = 0; i < ids.length; i++) {
				pst.setInt(1, ids[i]);
				System.out.println(ids[i]);
				//添加到批处理队列中
				pst.addBatch();
			}
			pst.executeBatch();
		});
		
	}

}
